package cn.edu.gdpt.mobile0123456789hwh0330;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
//1、全局控件变量声明
    private RadioGroup radGupMainCarType;
    private RadioButton radBtnMainSaloon;
    private RadioButton radBtnMainMPV;
    private RadioButton radBtnMainSUV;
    private RadioGroup radGupMainRooms;
    private RadioButton radBtnMainRoom1;
    private RadioButton radBtnMainRoom2;
    private RadioButton radBtnMainRoom3;
    private RadioButton radBtnMainRoom4;
    private Button btnMainCreateLine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//3、调用初始化方法initView()
//        4、给第一组单选项设置选中状态改变的监听器
        radGupMainCarType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton=findViewById(i);//通过参数i,获得当前被选中项的控件
                Log.i("radGupMainCarType", radioButton.getText().toString());
            }
        });
//        5、给第一组单选项设置选中状态改变的监听器
        radGupMainRooms.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton=findViewById(i);//通过参数i,获得当前被选中项的控件
                Log.i("radGupMainRooms", radioButton.getText().toString());
            }
        });
//        6、给按钮控件设置点击事件监听器，实现获取单选项数据功能
        btnMainCreateLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg="";//定义用于存储信息的字符串变量
                int carTypeId=radGupMainCarType.getCheckedRadioButtonId();//获取被选中的单选现ID
                if(carTypeId!=-1){//判断是否有杯选中
                    msg=((RadioButton)findViewById(carTypeId)).getText().toString();//获取被选中的文本
                }else {
                    msg="未选车型";
                }
                int roomId=radGupMainRooms.getCheckedRadioButtonId();
                if(roomId!=-1){
                    msg+=","+((RadioButton)findViewById(roomId)).getText().toString();
                }else {
                    msg+=", 未选生产车间";
                }
                Log.i("Button", msg);
            }
        });
    }
//2、 定义initView()初始化方法，调用findViewById()方法获取界面控件
    private void initView() {
        radGupMainCarType = (RadioGroup) findViewById(R.id.radGupMainCarType);
        radBtnMainSaloon = (RadioButton) findViewById(R.id.radBtnMainSaloon);
        radBtnMainMPV = (RadioButton) findViewById(R.id.radBtnMainMPV);
        radBtnMainSUV = (RadioButton) findViewById(R.id.radBtnMainSUV);
        radGupMainRooms = (RadioGroup) findViewById(R.id.radGupMainRooms);
        radBtnMainRoom1 = (RadioButton) findViewById(R.id.radBtnMainRoom1);
        radBtnMainRoom2 = (RadioButton) findViewById(R.id.radBtnMainRoom2);
        radBtnMainRoom3 = (RadioButton) findViewById(R.id.radBtnMainRoom3);
        radBtnMainRoom4 = (RadioButton) findViewById(R.id.radBtnMainRoom4);
        btnMainCreateLine = (Button) findViewById(R.id.btnMainCreateLine);
    }
}